/**
 * unavailabilities.cpp is part of %PROJECTNAME%.
 *
 * %PROJECTNAME% is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * %PROJECTNAME% is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with %PROJECTNAME%.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "unavailabilities.h"
#include "../../../utils/visitor/visitor.h"
#include <boost/foreach.hpp>
#include <algorithm>
#include <sstream>

///////////////////////////////////////////////////////////////
Unavailabilities::Unavailabilities() :
    ResourceConstraint(),
    m_unavailabilityPeriods()
{
}

///////////////////////////////////////////////////////////////
Unavailabilities::~Unavailabilities()
{
    m_unavailabilityPeriods.clear();
}

///////////////////////////////////////////////////////////////
void Unavailabilities::Accept( Visitor * a_visitor, bool )
{
    if( a_visitor )
        a_visitor->Visit( this );
}

///////////////////////////////////////////////////////////////
std::string Unavailabilities::Print() const
{
    std::stringstream strStream;
    strStream << "Unavailability periods : [";
    BOOST_FOREACH( const tUnavailabilityPeriod & a_period, m_unavailabilityPeriods )
    {
        strStream << "<" << a_period.first << "-" << a_period.second << ">";
    }
    strStream << "]";
    return strStream.str();
}

///////////////////////////////////////////////////////////////
void Unavailabilities::Add( const tUnavailabilityPeriod & a_unavailabilityPeriod )
{
    m_unavailabilityPeriods.push_back( a_unavailabilityPeriod );

    // We maintain the unavailability periods ordered by start-end.
    std::sort( m_unavailabilityPeriods.begin(), m_unavailabilityPeriods.end() );
}

///////////////////////////////////////////////////////////////
const tUnavailabilityPeriods & Unavailabilities::UnavailabilityPeriods() const
{
    return m_unavailabilityPeriods;
}

///////////////////////////////////////////////////////////////
tUnavailabilityPeriods & Unavailabilities::UnavailabilityPeriods()
{
    return m_unavailabilityPeriods;
}
